package cn.uexpo.lhz.controller.admin;

import cn.uexpo.lhz.biz.EquipmentTrafficBiz;
import cn.uexpo.lhz.biz.StaffBiz;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.common.utils.ImportExeclUtil;
import cn.uexpo.lhz.domain.EquipmentTraffic;
import cn.uexpo.lhz.domain.EquipmentTrafficExample;
import cn.uexpo.lhz.domain.Staff;
import cn.uexpo.lhz.domain.StaffExample;
import cn.uexpo.lhz.domain.dto.StaffEqpEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * @ClassName:  ImportStaffController
 * @Description:TODO(人员设备通行记录检测)
 * @author: 李冬建
 * @date:   2020年7月10日 上午10:28:40
 * @Copyright:  1.0.0
 */
@RestController
@RequestMapping("/admin/staffEqp")
public class StaffEqpController extends BaseController<Staff> {

    @Autowired
    private StaffBiz staffBiz;
    @Autowired
    private EquipmentTrafficBiz equipmentTrafficBiz;

    Map<String, Object> errorMap=new HashMap<String, Object>();

    /**
     * 导入
     * @return
     * @throws Exception
     */
    @SuppressWarnings({ "unchecked", "unused" })
    @RequestMapping(value = "/import",  method = { RequestMethod.GET, RequestMethod.POST })
    public String importData() throws Exception {
        String msg = "";

        /******** 导入数据业务处理begin*********************/
        InputStream inputStream=null;
        try {
            inputStream = new FileInputStream("d:/eqpPass.xlsx");
            Workbook wb = WorkbookFactory.create(inputStream);
            // 读取对象列表的信息
            StaffEqpEntity record = new StaffEqpEntity();
            // 第二行开始，到倒数第三行结束（总数减去两行）
            List<StaffEqpEntity> records = ImportExeclUtil.readDateListT(wb, record, 2, 0);
            System.out.println(records + "\n\n" + records.size());
            int i=0;
            if (records != null && records.size() > 0) {
                for (StaffEqpEntity eqp : records) {
                     String staffName = eqp.getStaffName();//人员姓名
                     Date passTime = eqp.getPassTime();//通行日期
                     String flag = eqp.getFlag();//判断标记：1判断0或空不判断
                     int staffId = 0;
                     if (StringUtils.isNotBlank(flag)&&flag.trim().equals("1")) {
                         if (StringUtils.isNotBlank(staffName)) {
                             StaffExample example = new StaffExample();
                             StaffExample.Criteria citeria = example.createCriteria();
                             citeria.andStaffNameEqualTo(staffName);
                             List<Staff> staffs = staffBiz.selectByExample(example);
                             int size=staffs.size();
                             if(staffs!=null&&size>0){
                                 if(size>1){
                                     i++;
                                     String errorMsg="=============找到多条记录："+staffName;
                                     errorMap.put("errorMsg"+i, errorMsg);
                                 }else{
                                     Staff staff =staffs.get(0);
                                     staffId=staff.getStaffId();
                                     EquipmentTrafficExample example1 = new EquipmentTrafficExample();
                                     EquipmentTrafficExample.Criteria citeria1 = example1.createCriteria();
                                     citeria1.andStaffidEqualTo(staffId+"");
                                     List<EquipmentTraffic> traffics = equipmentTrafficBiz.selectByExample(example1);
                                     if (traffics != null && traffics.size() > 0) {
                                         for (EquipmentTraffic traffic:traffics) {
                                             Date traffictime= traffic.getTraffictime();
                                             int result=passTime.compareTo(traffictime);
                                             if(result==0){
                                                 String successMsg="---对比成功："+staffName;
                                                 break;
                                             }
                                         }
                                     }else{
                                         i++;
                                         String errorMsg="=============未找到："+staffName;
                                         errorMap.put("errorMsg"+i, errorMsg);
                                     }
                                 }
                             }
                         }
                     }


                }


            }
            inputStream.close();
            msg="操作成功！";
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.info("errorMap:"+errorMap);


        return msg;

    }



}
